# -*- coding: utf-8 -*-
#
# Copyright © 2009 Benedikt Eger
#
# This file is part of top-news-buzz.
#
# top-news-buzz is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# top-news-buzz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with top-news-buzz.  If not, see <http://www.gnu.org/licenses/>.

from google.appengine.ext import db

class ConfigValue(db.Model):
    value = db.StringProperty()

class MetricConfigValue(db.Model):
    value = db.StringProperty()

class MetricConfigEntry(db.Model):

    INPUT_TYPE_TEXT = 0
    INPUT_TYPE_RADIO = 1

    datastore_value = db.StringProperty()

    def __init__(self, name, default_value, title, description, input_type, metric):
        self.name = name
        self.title = title
        self.description = description
        self.input_type = input_type
        self.metric = metric
        self.value = self.__value_from_datastore(default_value)

    @staticmethod
    def store(key, value):
        metric_config_value = MetricConfigValue.get_or_insert(key)
        metric_config_value.value = value
        metric_config_value.put()

    def parameter_name(self):
        return self.__create_key()

    def __value_from_datastore(self, default_value):
        metric_config_value = MetricConfigValue.get_or_insert(self.__create_key())
        return metric_config_value.value if metric_config_value.value is not None else default_value

    def __create_key(self):
        return ('%s@@@%s' % (self.name, self.metric.__module__))

class Config:

    @staticmethod
    def language_code():
        return Config.__get_config_string_value('language_code', 'en_us')

    @staticmethod
    def articles_to_display():
        return Config.__get_config_int_value('articles_to_display', 20)

    @staticmethod
    def display_last_n_days():
        return Config.__get_config_int_value('display_last_n_days', 1)

    @staticmethod
    def articles_to_score_per_pass():
        return Config.__get_config_int_value('articles_to_score_per_pass', 10)

    @staticmethod
    def score_delay_minutes():
        return Config.__get_config_int_value('score_delay_minutes', 180)

    @staticmethod
    def feed_retrieval_delay_minutes():
        return Config.__get_config_int_value('feed_retrieval_delay_minutes', 60)

    @staticmethod
    def strip_html():
        return Config.__get_config_boolean_value('strip_html', True)

    @staticmethod
    def cleanup_score_limit():
        return Config.__get_config_float_value('cleanup_score_limit', 0.0)
        
    @staticmethod
    def get_feeds():
        from topnewsbuzz.feed import Feed
        return Feed.all()

    @staticmethod
    def __get_config_int_value(key_name, default_value):
        config = ConfigValue.get_by_key_name(key_name)
        if config is None or config.value == '':
            return default_value
        else:
            return int(config.value)

    @staticmethod
    def __get_config_float_value(key_name, default_value):
        config = ConfigValue.get_by_key_name(key_name)
        if config is None or config.value == '':
            return default_value
        else:
            return float(config.value)
    
    @staticmethod
    def __get_config_boolean_value(key_name, default_value):
        config = ConfigValue.get_by_key_name(key_name)
        if config is None or config.value == '':
            return default_value
        else:
            return True if config.value == 'True' else False

    @staticmethod
    def __get_config_string_value(key_name, default_value):
        config = ConfigValue.get_by_key_name(key_name)
        if config is None or config.value == '':
            return default_value
        else:
            return str(config.value)
